package com.iampzh.springvue.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iampzh.springvue.comm.MyResult;
import com.iampzh.springvue.entity.LoginUser;
import com.iampzh.springvue.entity.Role;
import com.iampzh.springvue.entity.User;
import com.iampzh.springvue.listener.ExcelReadListener;
import com.iampzh.springvue.listener.RoleExcelReadListener;
import com.iampzh.springvue.mapper.RoleMapper;
import com.iampzh.springvue.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@CrossOrigin
@RequestMapping("/role")
@RestController
public class RoleController {
    @Autowired
    private RoleMapper roleMapper;


    @GetMapping("/users")
    public List<Role> findAllUser(){
        return roleMapper.selectList(null);
    }
    @PostMapping("/save")
    public Integer save(@RequestBody Role role){
        if(role.getId()==null){
            return roleMapper.insert(role);
        }else{
            return roleMapper.updateById(role);
        }

    }
    @DeleteMapping("/{id}")
    public Integer delete(@PathVariable Integer id){
        return roleMapper.deleteById(id);
    }
    @GetMapping("/page")
    public Page<Role> findUsersPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize,
                                    @RequestParam(defaultValue = "") String name
    ){
        Page<Role> page=new Page<>(pageNum,pageSize);
        QueryWrapper<Role> queryWrapper=new QueryWrapper<>();
        if(!"".equals(name)){
            queryWrapper.like("name",name);
        }

        queryWrapper.orderByDesc("id");
        return roleMapper.selectPage(page,queryWrapper);
    }
    @PostMapping("deleteIds")
    public Integer deleteIds(@RequestBody List<Integer> ids){


        return roleMapper.deleteBatchIds(ids);
    }
    @GetMapping("/export")
    public void writeExcel(HttpServletResponse response) throws IOException {
        List<Role> users = roleMapper.selectList(null);

        EasyExcel.write("d:/用户角色.xlsx",User.class).sheet("用户角色表").doWrite(users);

    }
    @PostMapping("/readExcel")
    public void readExcel(@RequestParam MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(),Role.class,new RoleExcelReadListener(roleMapper)).sheet().doRead();

    }
//

//    @PostMapping("/register")
//    public MyResult register(@RequestBody LoginUser user){
//        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("username",user.getUsername());
//        List<User> users = userMapper.selectList(queryWrapper);
//
//        if(users.size()>0){
//            return new MyResult("405","用户名已存在",null);
//        }else{
//            User user2 = new User();
//            user2.setUsername(user.getUsername());
//            user2.setPassword(user.getPassword());
//            userMapper.insert(user2);
//            return MyResult.success();
//        }
//    }
}
